package com.dd.cloud.user.service.log.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.utils.IPUtil;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.user.entity.log.LogModul;
import com.dd.cloud.user.entity.log.LogOper;
import com.dd.cloud.user.mapper.log.LogOperMapper;
import com.dd.cloud.user.mq.MQConfig;
import com.dd.cloud.user.mq.RabbitSend;
import com.dd.cloud.user.req.log.OperLogPageReq;
import com.dd.cloud.user.res.log.OperLogPageRes;
import com.dd.cloud.user.service.log.ILogModulService;
import com.dd.cloud.user.service.log.ILogOperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 操作日志 服务实现类
 * </p>
 *
 * @author czg
 * @since 2022-04-27
 */
@Service
public class LogOperServiceImpl extends ServiceImpl<LogOperMapper, LogOper> implements ILogOperService {
    @Autowired
    private RabbitSend rabbitSend;
    @Autowired
    private ILogModulService logModulService;

    /**
     * 添加操作日志
     *
     * @param logOper
     * @return
     */
    @Override
    public int addLog(LogOper logOper) {
        if (logOper == null) {
            return 0;
        }
        if (logOper.getOperUrl() != null && logOper.getOperUrl().length() > 100) {
            logOper.setOperUrl(logOper.getOperUrl().substring(0, 100));
        }
        if (logOper.getOperResParams() != null && logOper.getOperResParams().length() > 2048) {
            logOper.setOperResParams(logOper.getOperResParams().substring(0, 2048));
        }
        if (logOper.getOperReqParams() != null && logOper.getOperReqParams().length() > 512) {
            logOper.setOperReqParams(logOper.getOperReqParams().substring(0, 512));
        }
        if (logOper.getOperMethod() != null && logOper.getOperMethod().length() > 128) {
            logOper.setOperMethod(logOper.getOperMethod().substring(0, 128));
        }
        if (logOper.getOperDesc() != null && logOper.getOperDesc().length() > 200) {
            logOper.setOperDesc(logOper.getOperDesc().substring(0, 200));
        }
        if (logOper.getRemarks() != null && logOper.getRemarks().length() > 200) {
            logOper.setRemarks(logOper.getRemarks().substring(0, 200));
        }
        if (logOper.getOperUrl() != null) {
            LogModul logModul = logModulService.getLogModulByUrl(logOper.getOperUrl());
            logOper.setModular(logModul.getModular());
            logOper.setOperType(logModul.getType());
            logOper.setOperDesc(logModul.getOperDesc());
            logOper.setRemarks(logModul.getRemarks());
        }
        IPUtil.IpAddr ipAddr = IPUtil.getIpAddr(logOper.getOperIp());
        if (ipAddr != null) {
            logOper.setOperIpAddr(ipAddr.getDetail());
        }
        boolean i = false;
        try {
            i = save(logOper);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i ? 1 : 0;
    }

    /**
     * 将操作日志添加进消息队列 MQ
     *
     * @param logOper
     */
    @Override
    public void addOperLogMQ(LogOper logOper) {
        rabbitSend.sendMessage(MQConfig.LOG_OPER_EXCHANGE, MQConfig.LOG_OPER_QUERE, logOper);
    }

    /**
     * 查询操作日志分页
     *
     * @param req
     * @return
     */
    @Override
    public IPage<OperLogPageRes> queryOperLogPage(OperLogPageReq req) {
        if (req.verify()) {
            throw new ZekeException(400, "日期参数有误");
        }
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();

        IPage<OperLogPageRes> iPage = req.pagePojo();
        iPage = getBaseMapper().queryOperLogPage(iPage, req);
        //智尔康管理员, 日志参数字段有手机号,身份证号等敏感信息, 去掉此字段
        if (loginInfo.getType() == LoginInfo.UserType.ADMIN && loginInfo.getId() == 33) {
            List<OperLogPageRes> collect = iPage.getRecords().stream().map(item -> {
                item.setOperReqParams(null);
                item.setOperResParams(null);
                return item;
            }).collect(Collectors.toList());
            iPage.setRecords(collect);
        }
        return iPage;
    }
}
